package com.ruoyi.mentor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.mentor.bo.StudentDetailAddBo;
import com.ruoyi.mentor.bo.StudentDetailEditBo;
import com.ruoyi.mentor.bo.StudentDetailQueryBo;
import com.ruoyi.mentor.domain.StudentDetail;
import com.ruoyi.mentor.mapper.StudentDetailMapper;
import com.ruoyi.mentor.service.IStudentDetailService;
import com.ruoyi.mentor.vo.StudentDetailVo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 研究生详细信息Service业务层处理
 *
 * @author LH
 * @date 2021-05-21
 */
@Service
public class StudentDetailServiceImpl extends ServiceImpl<StudentDetailMapper, StudentDetail> implements IStudentDetailService {

    @Override
    public StudentDetailVo queryById(Long id){
        return getVoById(id, StudentDetailVo.class);
    }

    @Override
    public TableDataInfo<StudentDetailVo> queryPageList(StudentDetailQueryBo bo) {
        PagePlus<StudentDetail, StudentDetailVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), StudentDetailVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<StudentDetailVo> queryList(StudentDetailQueryBo bo) {
        return listVo(buildQueryWrapper(bo), StudentDetailVo.class);
    }

    private LambdaQueryWrapper<StudentDetail> buildQueryWrapper(StudentDetailQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StudentDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStuId()!=null, StudentDetail::getStuId, bo.getStuId());
        lqw.eq(StrUtil.isNotBlank(bo.getStuResidenceAddress()), StudentDetail::getStuResidenceAddress, bo.getStuResidenceAddress());
        lqw.eq(StrUtil.isNotBlank(bo.getStuWorkingCompany()), StudentDetail::getStuWorkingCompany, bo.getStuWorkingCompany());
        lqw.eq(StrUtil.isNotBlank(bo.getStuResume()), StudentDetail::getStuResume, bo.getStuResume());
        lqw.eq(StrUtil.isNotBlank(bo.getStuReward()), StudentDetail::getStuReward, bo.getStuReward());
        lqw.eq(StrUtil.isNotBlank(bo.getStuMobile()), StudentDetail::getStuMobile, bo.getStuMobile());
        lqw.eq(StrUtil.isNotBlank(bo.getStuEmail()), StudentDetail::getStuEmail, bo.getStuEmail());
        lqw.eq(StrUtil.isNotBlank(bo.getStuUgSchool()), StudentDetail::getStuUgSchool, bo.getStuUgSchool());
        lqw.eq(StrUtil.isNotBlank(bo.getStuUgMajor()), StudentDetail::getStuUgMajor, bo.getStuUgMajor());
        lqw.eq(StrUtil.isNotBlank(bo.getStuUgTime()), StudentDetail::getStuUgTime, bo.getStuUgTime());
        lqw.eq(StrUtil.isNotBlank(bo.getStuPgFirstWishSchool()), StudentDetail::getStuPgFirstWishSchool, bo.getStuPgFirstWishSchool());
        lqw.eq(StrUtil.isNotBlank(bo.getStuPgFirstWishMajor()), StudentDetail::getStuPgFirstWishMajor, bo.getStuPgFirstWishMajor());
        lqw.eq(StrUtil.isNotBlank(bo.getStuPgFirstWishDirection()), StudentDetail::getStuPgFirstWishDirection, bo.getStuPgFirstWishDirection());
        lqw.eq(bo.getStuPgExamForeignScore() != null, StudentDetail::getStuPgExamForeignScore, bo.getStuPgExamForeignScore());
        lqw.eq(bo.getStuPgExamPolitical() != null, StudentDetail::getStuPgExamPolitical, bo.getStuPgExamPolitical());
        lqw.eq(bo.getStuPgExamMath() != null, StudentDetail::getStuPgExamMath, bo.getStuPgExamMath());
        lqw.eq(bo.getStuPgExamSchool() != null, StudentDetail::getStuPgExamSchool, bo.getStuPgExamSchool());
        lqw.eq(bo.getStuPgExamTotal() != null, StudentDetail::getStuPgExamTotal, bo.getStuPgExamTotal());
        lqw.eq(bo.getStuPgReview() != null, StudentDetail::getStuPgReview, bo.getStuPgReview());
        lqw.eq(bo.getStuPgTotal() != null, StudentDetail::getStuPgTotal, bo.getStuPgTotal());
        lqw.eq(StrUtil.isNotBlank(bo.getStuPgSecondMajor()), StudentDetail::getStuPgSecondMajor, bo.getStuPgSecondMajor());
        lqw.eq(StrUtil.isNotBlank(bo.getStuPgSecondDirection()), StudentDetail::getStuPgSecondDirection, bo.getStuPgSecondDirection());
        lqw.eq(StrUtil.isNotBlank(bo.getStuSourceType()), StudentDetail::getStuSourceType, bo.getStuSourceType());
        lqw.eq(StrUtil.isNotBlank(bo.getStuDependence()), StudentDetail::getStuDependence, bo.getStuDependence());
        lqw.eq(StrUtil.isNotBlank(bo.getStuComment()), StudentDetail::getStuComment, bo.getStuComment());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(StudentDetailAddBo bo) {
        StudentDetail add = BeanUtil.toBean(bo, StudentDetail.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(StudentDetailEditBo bo) {
        StudentDetail update = BeanUtil.toBean(bo, StudentDetail.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(StudentDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
